package LeetCode.leetcode151;

import java.util.Scanner;
import java.util.Stack;

public class Review1 {
    public String reverseWords(String s) {
        StringBuilder res = new StringBuilder();
        Stack<String> stack = new Stack<>();
        String[] arr = s.split(" ");
        for (int i = 0; i < arr.length; i++) {
            stack.push(arr[i]);
        }
        for (int i = 0; i < arr.length; i++) {
            if (stack.peek().trim().length() != 0){
                res.append(stack.pop()).append(" ");
            }else{
                stack.pop();
            }
        }
        return res.toString().trim();
    }
}
